package model.evaluate.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;

@Controller
@RequestMapping("/upload")
public class UploadController {
    @CrossOrigin
    @RequestMapping("/upload")
    @ResponseBody
    public String upload(@RequestParam("file") MultipartFile file, HttpServletRequest req){
//        System.out.println(file);
        System.out.println(file.getOriginalFilename());
        try {
            String path = req.getServletContext().getRealPath("/");
            file.transferTo(new File(path+"/imgs/"+file.getOriginalFilename()));
            return path;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "0";
    }
}
